<?php
/*
 *总后台公共函数
 */

function is_login(){
	$adminid=!empty($_SESSION['adminid'])?$_SESSION['adminid']:'';
	if(!empty($adminid)){
		return $adminid;
	}else{
		return 0;
	}
}

/*
 * 获取分页所需要的参数
 * @param $page
 * @param $pagesize
 * @param $count
 * 
 * return array
 *   */
    function getPageArr($page,$pagesize=10,$count) {
    $page           = isset($page)?abs(intval($page)):1;
    $pagesize       = isset($pagesize)?abs(intval($pagesize)):10;
    $page > 100000 && $page = 100000;
    $pagesize > 100 && $page = 100;
    $pagetotal      = ceil($count / $pagesize);
    ($page>$pagetotal) && ($page = $pagetotal);
    ($page<1) && ($page = 1);
    
    //下一页
    if($page>$pagetotal){
      $nextpage=$pagetotal;
    }else{
        $nextpage=$page+1;
    }
    //上一页
    if($page>1){
      $prevpage=$page-1;
    }else{
      $prevpage=$page;
    }

    $pagearr = array(
        "page"      =>$page,
        "pagesize"  =>$pagesize,
        "pagetotal"=>$pagetotal,
        "count" =>$count,
        "start"=>($page - 1) * $pagesize,
        "nextpage"=>$nextpage,
        "prevpage"=>$prevpage

    );
    return $pagearr;
    }

//公共图片处理上传类

     function imgUpload($config)
    {
        $maxsize=$config['maxsize'];
        $path=$config['path'];
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     $maxsize ;// 设置附件上传大小
        $upload->maxwidth  =$config['maxwidth'];
        $upload->maxheight  =$config['maxheight'];
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        //$upload->rootPath  =     '/Public/Supplier/Admin/image/upload/'; // 设置附件上传根目录
        $upload->rootPath  =     $path; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();
        if(!$info) 
        {// 上传错误提示错误信息
           // $this->error($upload->getError());
           return $upload->getError();
        }else
        {// 上传成功
            return $info;
        }
     }
    

        /**
     *
     * 返回已经确定子分类的类别属性，计算等级
     *
     */
     function getListCat($data = array(),$level = 0) {
        $cats = array();
        !$data && $data =getCats();
        if(!$data) return $cats;
        foreach($data as $key => $val) {
            $cats[$val['typeid']] = &$data[$key];
            $cats[$val['typeid']]['level'] = $level;
            if( isset( $val['children'] ) ) {
                $cats[$val['typeid']]['haschild'] = 1;
                $cats += getListCat($val['children'],$level + 1);
                unset($data[$key]['children']);
            } 
        }
        return $cats;
    }
    
    /**
     *
     * 获取所有类别
     *
     */
     function getCats($return = 'tree') {
        static $retarr;
        if(isset($retarr[$return])) return $retarr[$return];

        $cats = M('type')->order('sort')->getField('typeid,typename,pid,sort');
        if($return == 'tree')
        {           //返回数组
            $tree = array();

            foreach($cats as $cat)
             {  //循环cat
                if(isset($cats[$cat['pid']])) 
                {//非第一级
                    $cats[$cat['pid']]['children'][] = &$cats[$cat['typeid']];
                } else
                 {//第一级
                    $tree[] = &$cats[$cat['typeid']];

                }
            }
            $retarr[$return] = $tree;
        } else if($return == 'source')
         {//返回source
            $retarr[$return] = $cats;
        }
        return $retarr[$return];
    }
